package com.mud.mudcommune.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.mud.mudcommune.R;
import com.mud.mudcommune.view.fragment.OrderFragment;
import com.mud.mudcommune.view.fragment.twoLevel.OrderDetailFragment;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by zhoujin on 2016/12/8.
 */

public class OrderAdapter extends MyBaseAdapter {
    private List list;
    private Context context;
    private static FragmentManager fragmentManager;
    public OrderAdapter(Context context, @NonNull List list,FragmentManager fragmentManager) {
        super(list);
        this.fragmentManager=fragmentManager;
        this.list = list;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.order_item, null);
             viewHolder = getViewHolder(convertView);
        }
        viewHolder = getViewHolder(convertView);
        viewHolder.name.setText("王志学");
        viewHolder.square.setText("楼盘暂无 | 127m");
        viewHolder.number.setText("订单编号：F1624000659");
        viewHolder.status.setText("跟进状态：前期沟通");
        return convertView;
    }
    public ViewHolder getViewHolder(View convertView) {
        ViewHolder viewHolder = null;
        viewHolder = (ViewHolder) convertView.getTag();
        if (viewHolder == null) {
            viewHolder = new ViewHolder(convertView);
        }
        return viewHolder;
    }

    static class ViewHolder implements View.OnClickListener {
         TextView name;
         TextView square;
         TextView status;
         TextView number;
         Button orderDetails;
        public ViewHolder(View convertView){
            name = ButterKnife.findById(convertView,R.id.order_name);
            square=ButterKnife.findById(convertView,R.id.order_square);
            status =  ButterKnife.findById(convertView,R.id.order_status);
            number =  ButterKnife.findById(convertView,R.id.order_number);
            orderDetails= ButterKnife.findById(convertView,R.id.order_button);
            orderDetails.setOnClickListener(this);
            convertView.setTag(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.order_button:
                    fragmentManager.beginTransaction()
                            .hide(OrderFragment.getInstance())
                            .add(R.id.main_frame, OrderDetailFragment.getInstance())
                            .addToBackStack(null)
                            .commit();
                    break;
            }
        }
    }
}
